/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BUS;
import DTO.*;
import DAO.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author ldtung_it
 */
public class TasksBUS {
    public static ArrayList<TasksDTO> getTask(Date date,int idContact) throws SQLException{
        return TasksDAO.getTask(date,idContact);
    }
    public static ArrayList<TasksDTO> getTaskMonth(int month, int year, int idContact){
        try {
            return TasksDAO.getTaskMonth(month, year, idContact);
        } catch (SQLException ex) {
            Logger.getLogger(TasksBUS.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }
    public static void updateTask(Calendar date,int idContact,String newEvent){
        try {
            int result = TasksDAO.update(date, idContact, newEvent);
            if(result != 0)
               JOptionPane.showMessageDialog(null, "Update successfully");
            else
                JOptionPane.showMessageDialog(null, "Have problems with update action");
        } catch (SQLException ex) {
            Logger.getLogger(TasksBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void deleteTask(Calendar date,int idContact){
        try {
            int result = TasksDAO.delete(date, idContact);
            if(result != 0)
               JOptionPane.showMessageDialog(null, "Deleted successfully");
            else
                JOptionPane.showMessageDialog(null, "Have problems with delete action");

        } catch (SQLException ex) {
            Logger.getLogger(TasksBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void addTask(TasksDTO task){
        String error = "";
        if(!CheckData.isNum(task.getIDContact())){
            error += "  IDContact must be number\n";
        }
        if(!CheckData.isNum(task.getIDCount())){
            error += "  IDTask must be number\n";
        }
        if(task.getEvent() == null){
            error += "  Event content must not null\n";
        }
        if(error.length() != 0){
            JOptionPane.showMessageDialog(null, error);
            return ;
        }
        try {
            int result = TasksDAO.add(task);
            if(result != 0)
                JOptionPane.showMessageDialog(null, "Added successfully");
            else
                JOptionPane.showMessageDialog(null, "Have problems with add action");
        } catch (SQLException ex) {
            Logger.getLogger(TasksBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
